import os

# 拼接地址
from time import sleep

import faker
import yaml
from faker import Faker


def join_path(directory: str, filename: str) -> str:
    abspath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), directory, filename)

    return abspath


# yml读取
def get_yaml(filename):
    sleep(1)
    filename = join_path("data", filename)
    with open(filename, 'r', encoding="utf8") as file:
        data = yaml.safe_load(file)

    return data


def set_yaml(num, filename):
    fake: Faker = Faker(locale='zh_CN')
    user_info = [{"name": fake.name(), "phone": fake.phone_number()} for i in range(num)]
    filename = join_path('data', filename)
    with open(filename, 'w', encoding='utf8') as file:
        yaml.safe_dump(user_info, file)


def case_user_name(data):
    user_name_list = [list(data[i].values())[0] for i in range(len(data))]

    return user_name_list


if __name__ == '__main__':
    set_yaml(3, "mock_user_info.yml")
    user_data = get_yaml("mock_user_info.yml")
    list_name = case_user_name(user_data)
    print(list_name)